<?php
class Admin extends CI_Controller{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->model('server_info_model');
		$this->load->model('holiday_info_model');
		$this->load->model('employe_info_model');
		$this->load->model('employe_info_model');
		$this->load->model('emp_ser_count_model');
		$this->load->library('upload');
		$this->load->library('session');
	}

	function login(){
		$this->load->view('admin/login_view');
	}
	function index(){
		$this->load->view('admin/login_view');
	}
	function main(){
		$data = $this->employe_info_model->isLogin($_POST['username'],$_POST['password']);
		if($data)
		{
			if("admin"==$_POST['username']){
				$sess_data = array(
					'login_s' => True,
					'isAdmin' => True
				);
			}else {
				$sess_data = array(
					'login_s' => True
				);
			}
			$this->session->set_userdata($sess_data);
			$this->load->view('admin/index_view',$sess_data);
		}
		else {
			$data['errMsg']="<script language='javascript'>alert('username not exist or password is wrong!');</script>";
			$this->load->view('admin/login_view',$data);
		}

	}
	function login_out(){
		$this->session->sess_destroy();
		redirect('/admin','refresh');
	}
	function updatePwd(){
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		$data['login_s']=$this->session->userdata('login_s');
		$data['email']=$this->session->userdata('username');
		$this->load->view('admin/pwd_update_view',$data);
	}
	function pwd_doUpdate(){
		$this->load->view('admin/server_add_view');
	}

	function left(){
		$this->load->view('admin/left_view');
	}
	function top(){
		$this->load->view('admin/top_view');
	}
	function employeeMgr(){
		$this->load->view('admin/employeeMgr_view');
	}
	function employeeMgr_add(){
		$this->load->view('admin/employeeMgr_edit_view');
	}
	function welcome(){
		redirect('booking/getBookInfo','refresh');
	}

	function server(){
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		$data['query']=$this->server_info_model->getAllServerInfo();
		$this->load->view('admin/server_view', $data);
	}
	function server_add(){
		$this->load->view('admin/server_add_view');
	}
	function empSerCountInfo(){
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		$sql = "SELECT a.id, b.employena, c.serverna, a.sercount
		FROM emp_ser_count a
		LEFT JOIN employe_info b ON a.employeid = b.employeid
		LEFT JOIN server_info c ON a.serverid = c.serverid ";
		$data['query']=$this->db->query($sql);
		//$data['query']=$this->emp_ser_count_model->getAllEmpSerCount();
		$this->load->view('admin/emp_ser_count_view', $data);
	}
	function empSerCount_add(){
		//$sql='select employeid, employena from employe_info where employeid not in(select employeid from emp_ser_count)';
		$sql='select employeid, employena from employe_info ';
		$data['query']=$this->employe_info_model->db->query($sql);
		$data['serverinfo']=$this->employe_info_model->db->query("select serverid,serverna from server_info");
		$this->load->view('admin/emp_ser_count_add_view',$data);
	}
	function empSerCount_update(){
		$id = $this->uri->segment(3);
		$data['query']=$this->emp_ser_count_model->getEmpSerCount($id);
		$this->load->view('admin/emp_ser_count_edit_view',$data);
	}

	function empSerCount_doUpdate(){
		$data=array(
				'id'=>$_POST['id'],	
				'employeid'=>$_POST['employeid'],
				'serverid'=>$_POST['serverid'], 
				'sercount'=>$_POST['sercount']
		);
		$id = $this->emp_ser_count_model->updateEmpSerCount($_POST['id'],$data);
		redirect('admin/empSerCountInfo/index','refresh');
	}


	function empSerCount_del(){
		$id = $this->uri->segment(3);
		$this->emp_ser_count_model->delEmpSerCount($id);
		redirect('admin/empSerCountInfo/index','refresh');
	}
	function empSerCount_doAdd(){
		$id = $this->emp_ser_count_model->addEmpSerCount();
		redirect('admin/empSerCountInfo/index','refresh');
	}

	function server_doAdd(){
		$id = $this->server_info_model->addServerInfo();
		redirect('admin/server/index','refresh');
	}
	function server_update(){
		$id = $this->uri->segment(3);
		$data['query']=$this->server_info_model->getServerInfo($id);
		$this->load->view('admin/server_edit_view',$data);
	}
	function server_doUpdate(){
		$data=array(
				'serverna'=>$_POST['serverna'],
				'servertime'=>$_POST['servertime'], 
		);
		$id = $this->server_info_model->updateServerInfo($_POST['serverid'],$data);
		redirect('admin/server/index','refresh');
	}
	function server_del(){
		$id = $this->uri->segment(3);
		$this->server_info_model->delServerInfo($id);
		redirect('admin/server/index','refresh');
	}

	function holiday(){
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		$data['query']=$this->holiday_info_model->getAllHolidayInfo();
		$this->load->view('admin/holiday_view', $data);
	}
	function holiday_add(){
		$this->load->view('admin/holiday_add_view');
	}

	function holiday_doAdd(){
		$id = $this->holiday_info_model->addHolidayInfo();
		redirect('admin/holiday/index','refresh');
	}
	function holiday_update(){
		$id = $this->uri->segment(3);
		$data['query']=$this->holiday_info_model->getHolidayInfo($id);
		$this->load->view('admin/holiday_edit_view',$data);
	}
	function holiday_doUpdate(){
		$data=array(
				'holidayna'=>$_POST['holidayna'],
				'holidaytime'=>$_POST['holidaytime'], 
		);
		$id = $this->holiday_info_model->updateHolidayInfo($_POST['id'],$data);
		redirect('admin/holiday/index','refresh');
	}
	function holiday_del(){
		$id = $this->uri->segment(3);
		$this->holiday_info_model->delHolidayInfo($id);
		redirect('admin/holiday/index','refresh');
	}

	function upload_img()
	{
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		$this->load->view('admin/upload_form', array('error' => ' ' ));
	}

	function do_upload(){
		if( ! $this->session->userdata('login_s'))
		{
			redirect('admin/login','refresh');
		}
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('admin/upload_form', $error);
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());

			$this->load->view('admin/upload_success', $data);
		}

	}

	function style(){
		$query=$this->db->query("select stylestr from style_info");
		$row = $query->row_array();
		$data['style'] = $row['stylestr'];
		$this->load->view('admin/style_view',$data);
	}

	function updateStyle(){
		$style=$_POST['style'];

		$this->db->query("update style_info set stylestr ='".$style."'");
		$data['style']=$style;
		$this->load->view('admin/style_view',$data);
	}

}
?>